package com.action;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**贷款控制层**/
import org.springframework.web.servlet.ModelAndView;

import com.dto.Apply;
import com.dto.Fenqi;
import com.dto.Retmsg;
import com.dto.UserInfo;
import com.service.CreditService;

@RestController
public class CreditAction {
	@Autowired
	private CreditService cs;
		
	/**@param
	 * @return
	 * @用途:查询贷款记录---分页
	 */
	@RequestMapping("queryRetmsgInfo.do")
	public ModelAndView queryRetmsgInfo(HttpSession sess) {
		ModelAndView mv=new ModelAndView();
		UserInfo user=(UserInfo) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession().getAttribute("userInfo");
		/*UserInfo user=new UserInfo();
		user.setU_id("3")*/;
		sess.setAttribute("userInfo", user);
		List<Retmsg> rtList=cs.queryRetmsgInfo(user.getU_id());
		List<Fenqi> fq=cs.queryfenqqqishu();
		Apply app=cs.queryApplyById(user.getU_id());
		String sid=app.getS_id();
		sess.setAttribute("sid", sid);
		mv.addObject("rtList", rtList);
		mv.addObject("fq", fq);
		mv.setViewName("forward:Myloan.jsp");
		return mv;
	}
	
	/**@param
	 * @return
	 * @用途:还款
	 */
	@RequestMapping("repayment.do")
	public ModelAndView repayment(HttpSession sess) {//类型　具体的
		ModelAndView mv=new ModelAndView();
		String ret_type=(String)sess.getAttribute("ret_type");
		boolean bool=cs.rembursementById(ret_type);
		if(bool) {
			mv.setViewName("redirect:queryRetmsgInfo.do");
		}else {
			mv.setViewName("redirect:queryRetmsgInfo.do");
		}
		return mv;
	}	
	
	
	
	/**@param
	 * @return<第一次还款及最后一次还款>
	 * @用途:查询下一期贷款详情
	 */
	@RequestMapping("queryRepaymentInfo.do")
	public ModelAndView queryRepaymentInfo(HttpSession sess/*,String ret_type,String ret_num*/) {
		ModelAndView mv=new ModelAndView();
		UserInfo user=(UserInfo) sess.getAttribute("userInfo");
		/*UserInfo user=new UserInfo();
		user.setU_id("3");*/
		Retmsg retmsg=cs.queryNperById(user.getU_id());
		Double ret_money=0.0;
		if(retmsg==null) {//第一次还款
			retmsg=new Retmsg();
			retmsg.setNper(0);
			com.dto.Apply apply=cs.queryApply(user.getU_id());
			if(apply==null) {
				mv.setViewName("redirect:Mypayment.jsp");
			}else {
				ret_money=cs.queryRepaymentAmount(apply.getFq_id(), apply.getPay_num(), user.getU_id());
				retmsg.setRet_type(apply.getFq_id());//贷款
			}
		}else {
			ret_money=cs.queryRepaymentAmount(retmsg.getRet_type(), retmsg.getRet_num(), user.getU_id());
		}
		DecimalFormat df = new DecimalFormat("#.00");//保留2位小数
		List<com.dto.Fenqi> fenqi=cs.queryfenqqqishu();//-------------查询分期集合
		String money=df.format(ret_money);
		Date ret_date=cs.queryEdate(user.getU_id());
		mv.addObject("fenqi", fenqi);//期数
		mv.addObject("Nper", retmsg.getNper()+1);//期数
		mv.addObject("ret_date", ret_date);//还款时间
		mv.addObject("ret_money", money);//金钱
		mv.addObject("ret_type", retmsg.getRet_type());//贷款的类型
		mv.addObject("ret_sta", retmsg.getRet_sta());//状态
		mv.setViewName("forward:Mypayment.jsp");
		return mv;
	}
	
	/**@param
	 * @return
	 * @用途:查询已还贷款
	 */
	@RequestMapping("queryNewLoans.do")
	public ModelAndView queryNewLoans(HttpSession sess) {
		ModelAndView mv=new ModelAndView();
		UserInfo user=(UserInfo) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession().getAttribute("userInfo");
		/*UserInfo user=new UserInfo();
		user.setU_id("3");*/
		List<Retmsg> rList=cs.queryRetmsgInfo(user.getU_id());
		sess.setAttribute("rList", rList);
		/*mv.addObject("rList", rList);*/
		mv.setViewName("forward:Mypayment.jsp");
		return mv;
	}
	
	
	/**@param
	 * @return
	 * @用途:贷款统计
	 */
	@RequestMapping("LoanStatistics.do")
	public ModelAndView LoanStatistics(HttpSession sess) {
		ModelAndView mv=new ModelAndView();
		UserInfo user=(UserInfo) sess.getAttribute("userInfo");
		/*UserInfo user=new UserInfo();
		user.setU_id("3");*/
		Double m=0.0;
		try {
			m=cs.queryMoney(user.getU_id());
		}catch(Exception e) {
			e.printStackTrace();
		}
		String money="";
		if(m==null) {
			money="0.0";
		}else {
			DecimalFormat df = new DecimalFormat("#.00");//保留2位小数
			money=df.format(m);
		}	
		mv.addObject("money", money);//本金
		Retmsg retmsg=cs.queryNperById(user.getU_id());
		if(retmsg==null) {//用户没有贷款操作
			retmsg=new Retmsg();
			retmsg.setNper(0);
			/*retmsg.setRet_money(0.0);*/
		}
		mv.addObject("Nper", retmsg.getNper());//期数
		mv.addObject("ret_type", retmsg.getRet_type());//贷款的类型
		List<com.dto.Fenqi> fenqi=cs.queryfenqqqishu();//-------------查询分期集合
		mv.addObject("fenqi", fenqi);//期数
		mv.addObject("ret_money", retmsg.getRet_money());//金钱
		mv.setViewName("forward:Mycount.jsp");//贷款统计
		return mv;

	}
	
	/**@param
	 * @return
	 * @用途:点击还款后对余额的操作
	 */
	@RequestMapping("/payMoney.do")
	@ResponseBody
	public Map<String,Object> payMoney(String ret_type,Double ret_money,HttpSession session){
		Map<String,Object> json=new HashMap<String,Object>();
		UserInfo user=(UserInfo)session.getAttribute("userInfo");
		Double balance=user.getBalance();
		if(balance>=ret_money) {
			//扣款操作
			Double newbalance=balance-ret_money;
			user.setBalance(newbalance);
			int n=cs.updateBalanceById(user);
			if(n==1) {
				session.setAttribute("userInfo", user);
				session.setAttribute("ret_type", ret_type);
				json.put("result", true);
			}else {
				json.put("result", false);
			}
		}else {
			json.put("result", "errorMoney");
		}
		return json;
	}
	
}
